<template >
    <div>
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/menu/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item><a href="/menu/">权限管理</a></el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片 -->
        <el-card class="box-card">
            <!-- 添加 -->
            <el-button type="primary" @click="dialogFormVisible = true">添加</el-button>
            <!-- 表格 -->
            <el-table :data="tableData" style="width: 100%">
                <el-table-column type="expand">
                    <!-- 权限 tag  栅格实现 -->
                    <template slot-scope="scope">
                        <div class="rowcol">
                            <!-- 一级 -->
                            <el-row v-for="item1 in scope.row.children" :key="item1.id">
                                <el-divider></el-divider>
                                <el-col :span="3">
                                    <el-tag closable @close="deltag(scope.row,item1)">{{ item1.authName }}</el-tag>
                                    <i class="el-icon-caret-right"></i>
                                </el-col>
                                <el-col :span="21">
                                    <!-- 二级 -->
                                    <el-row v-for="item2 in item1.children" :key="item2.id">
                                        <el-divider></el-divider>
                                        <el-col :span="4">
                                            <el-tag type="success" @close="deltag(scope.row , item2)" closable>{{
                                            item2.authName }}</el-tag>
                                            <i class="el-icon-caret-right"></i>
                                        </el-col>
                                        
                                        <el-col :span="20">
                                            
                                            <!-- 三级 -->
                                            <el-tag type="warning" closable @close="deltag(scope.row,item3)"
                                                v-for="item3 in item2.children" :key="item3.id">{{
                                                item3.authName }}</el-tag>
                                        </el-col>
                                    </el-row>
                                </el-col>
                              
                            </el-row>                        
                        </div>

                    </template>
                </el-table-column>
                <el-table-column label="序号" type="index">
                </el-table-column>
                <el-table-column label="角色名称" prop="roleName">
                </el-table-column>
                <el-table-column label="角色描述" prop="roleDesc">
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" @click="update(scope.row)" size="mini">编辑
                        </el-button>
                        <el-button type="danger" icon="el-icon-delete" @click="del(scope.row)" size="mini">删除
                        </el-button>
                        <el-button type="warning" icon="el-icon-setting" @click="right(scope.row)" size="mini">分配权限
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <!-- 添加的模态框 -->
        <el-dialog title="添加" :visible.sync="dialogFormVisible">
            <el-form :rules="rules" ref="form" :model="form" label-width="80px">
                <el-form-item label="角色名称" prop="username">
                    <el-input v-model="form.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="角色描述" prop="desc">
                    <el-input v-model="form.desc" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="add()">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 编辑的模态框 -->
        <el-dialog title="编辑" :visible.sync="dialogFormVisible1">
            <el-form :rules="rules" ref="form" :model="form" label-width="80px">
                <el-form-item label="角色名称" prop="username">
                    <el-input v-model="form.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="角色描述" prop="desc">
                    <el-input v-model="form.desc" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible1 = false">取 消</el-button>
                <el-button type="primary" @click="do_update()">编 辑</el-button>
            </div>
        </el-dialog>
        <!-- 权限的模态框 -->
        <el-dialog title="分配权限" :visible.sync="dialogFormVisible2">
            <el-form label-width="80px">

                <!-- 树状图 -->
                <el-tree :data="treelist" :highlight-current="true" empty-text show-checkbox node-key="id" ref='treeRef'
                    default-expand-all :default-checked-keys="roleslist" :props="defaultProps">
                </el-tree>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible2 = false">取 消</el-button>
                <el-button type="primary" @click="do_right()">完 成</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
    name: 'roles',
    data() {
        return {

            tableData: [],
            treelist: [],
            roleslist:[],
            dialogFormVisible: false,
            dialogFormVisible1: false,
            dialogFormVisible2: false,
            form: {
                username: '',
                desc: '',
            },
            rules: {
                username: [
                    { required: true, message: '角色名称不能为空', trigger: 'blur' },
                ],
                desc: [
                    { required: true, message: '角色描述不能为空', trigger: 'blur' },
                ]
            },
            defaultProps: {
                children: 'children',
                label: 'authName'
            },
            id: 0,
            rid: 0,
            key: []
        }
    },
    created() {
        this.getlist()

    },
    methods: {
        async getlist() {
            var res = await this.axios({
                url: '/v1/roles',
            })
            console.log(res);
            this.tableData = res.data.data
        },
        // 添加
        add() {
            this.$refs.form.validate(async (valid) => {
                if (!valid) {
                    return this.$message.error('校验失败,爬')
                } else {
                    var res = await this.axios({
                        url: '/v1/roles',
                        method: "POST",
                        data: {
                            roleName: this.form.username,
                            roleDesc: this.form.desc
                        }
                    })
                    console.log(res);
                    if (res.data.meta.status == 201) {
                        this.$message.success('添加角色成功')
                        this.getlist()
                        this.dialogFormVisible = false
                        this.form = {}
                    }
                }
            })
        },
        // 删除
        del(row) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                var id = row.id``
                var res = await this.axios({
                    url: `/v1/roles/${id}`,
                    method: 'DELETE',
                })
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
                this.getlist()
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 删除单个标签
        async deltag(row, it) {

            let id = row.id
            let rid = it.id
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                var res = await this.axios({
                    url: `/v1/roles/${id}/rights/${rid}`,
                    method: 'DELETE',
                })
                console.log(res, 'res');
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
                // this.getlist()
                row.children = res.data.data
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        // 编辑
        update(row) {
            console.log(row);
            this.form.desc = row.roleDesc
            this.form.username = row.roleName
            this.id = row.id
            this.dialogFormVisible1 = true
        },
        async do_update() {
            let id = this.id
            var res = await this.axios({
                url: `/v1/roles/${id}`,
                method: "PUT",
                data: {
                    roleName: this.form.username,
                    roleDesc: this.form.desc
                }
            })
            console.log(res);
            if (res.data.meta.status == 200) {
                this.$message.success('编辑成功')
                this.getlist()
                this.dialogFormVisible1 = false
            }
        },
        // 权限
        async right(row) {
            this.dialogFormVisible2 = true
            this.rid = row.id
            var res = await this.axios({
                url: '/v1/rights/tree',

            })
            this.treelist = res.data.data
            this.getkeys(row,this.roleslist)
            console.log(this.treelist);
        },
        getkeys(row,arr){
         if(!row.children){
            return arr.push(row.id)
         }
         row.children.forEach((item)=>{
            this.getkeys(item,arr)
         })
        },
        async do_right() {
            this.dialogFormVisible2 = false
            var kpl = this.$refs.treeRef.getCheckedKeys()
            var kps =  this.$refs.treeRef.getHalfCheckedKeys()
            var rer = [...kpl,...kps].join(',')
            this.key = rer

            console.log(kpl);
            var res = await this.axios({
                url: `/v1/roles/${this.rid}/rights`,
                method: "POST",
                data: {
                    rids: rer
                }
            })
            console.log(res);
            if (res.data.meta.status == 200) {
                this.$message.success('更新成功')
                this.getlist()
            }
        }

    }
}
</script>
<style >
.box-card {
    width: 100%;
    margin-top:15px;
}
.el-table__cell{
    width: 100%;
}
.rowcol{
    width: 90%;
    margin: 0 auto;
}
.el-tag{
    margin: 3px;
}
/* li {
    width: 100%;
    height: 100%;
    list-style: none;
    display: flex;
    justify-content: space-around;
    align-items: center;


} */
</style>